﻿using FileTranslator;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;

namespace UnionPay
{
    static class LoaderHelper
    {
        static public void LoadFilesAndSetConstraints()
        {
            LoadFiles();
            
            // 增加外键约束
            // 需要注意的是，一个DataContainer才是真正承载数据的地方，所以只需要设置它的约束即可
            SetFmtForeignKeyConstraints();
            SetFldForeignKeyConstraints();

            // 验证外键约束
            if (fmtLoader.DataVessel.VerifyConstraint() == false)
            {
                // TODO: log
                throw new System.Exception("Constraint not qualified in Format files");
            }

            if (fldLoader.DataVessel.VerifyConstraint() == false)
            {
                // TODO: log
                throw new System.Exception("Constraint not qualified in Field files");
            }
        }

        private static void SetFldForeignKeyConstraints()
        {
            // fld相关外键约束
            fldLoader.DataVessel.ConstraintHelper.AddConstraint(
                    new FileTranslator.ForeignKeyConstraint(
                        "sour_fld_proc_cmd",
                        "func_index",
                        "func_name",
                        fldProccessLoader.ResultTable));

            // 读取.h的对应关系，
            //fldLoader.DataVessel.ConstraintHelper.AddConstraint(
            //    new FileTranslator.ForeignKeyConstraint(
            //        "elem_sour_fld",
            //        "elem_sour_fld",
            //        fldProccessLoader.DataVessel.GetDataTableWrapper(0).InnerTable));

            fldLoader.DataVessel.ConstraintHelper.AddConstraint(
                    new FileTranslator.ForeignKeyConstraint(
                        "elem_buss_id",
                        "buss_index",
                        "name",
                        elemBussIdLoader.ResultTable));

            fldLoader.DataVessel.ConstraintHelper.AddConstraint(
                    new FileTranslator.ForeignKeyConstraint(
                        "sour_fld_tp",
                        "code",
                        "name",
                        sourFldTpLoader.ResultTable));
        }

        private static void SetFmtForeignKeyConstraints()
        {
            // format相关外键约束
            DataTable dtFmtTypeTable = new DataTable();
            dtFmtTypeTable.Columns.Add("category_index");
            dtFmtTypeTable.Columns.Add("category");
            dtFmtTypeTable.Rows.Add(new object[] { 1, fmtLoader.DataVessel.Category });

            FileTranslator.ForeignKeyConstraint fkc = new FileTranslator.ForeignKeyConstraint(
                        "elem_index",
                        "elem_index",
                        "elem_desc",
                        fldLoader.ResultTable);
            fkc.AssociatedColumns.Add("elem_desc", "elem_desc");
            fkc.AssociatedColumns.Add("resv_fld", "resv_fld");
            fkc.AssociatedColumns.Add("orig_oper_in", "orig_oper_in");
            fkc.AssociatedColumns.Add("orig_ver_id", "orig_ver_id");
            fkc.AssociatedColumns.Add("rec_upd_usr_id", "rec_upd_usr_id");
            fkc.AssociatedColumns.Add("oper_in", "oper_in");
            fkc.AssociatedColumns.Add("ver_id", "ver_id");
            fmtLoader.DataVessel.ConstraintHelper.AddConstraint(fkc);

            fmtLoader.DataVessel.ConstraintHelper.AddConstraint(
                    new FileTranslator.ForeignKeyConstraint(
                        "fmt_tp",
                        "category_index",
                        "category",
                        dtFmtTypeTable));

            fmtLoader.DataVessel.ConstraintHelper.AddConstraint(
                    new ReadonlyConstraint("fmt_index"));

            fmtLoader.DataVessel.ConstraintHelper.AddConstraint(
                    new ReadonlyConstraint("fmt_nm"));
        }
        private static void LoadFiles()
        {
            try
            {
                // 格式文件加载器
                fmtLoader = new FmtFileLoader();
                fmtLoader.DataVessel = new FmtContainer();
                fmtLoader.TargetPath = UnionPay.Properties.Resources.FmtFilePath;
                fmtLoader.Load();

                // 字段文件加载器
                fldLoader = new FldFileLoader();
                fldLoader.DataVessel = new FldContainer();
                fldLoader.TargetPath = UnionPay.Properties.Resources.FldFilePath;
                fldLoader.Load();

                // 字段函数文件加载器
                fldProccessLoader = new FldProcessFileLoader();
                fldProccessLoader.DataVessel = new FldProcessContainer();
                fldProccessLoader.TargetPath = UnionPay.Properties.Resources.FldProcessorFilePath;
                fldProccessLoader.Load();

                // ElemBussId文件加载器
                elemBussIdLoader = new ElemBussIdFileLoader();
                elemBussIdLoader.DataVessel = new ElemBussIdContainer();
                elemBussIdLoader.TargetPath = UnionPay.Properties.Resources.ElemBussId;
                elemBussIdLoader.Load();

                // SourFldTp文件加载器
                sourFldTpLoader = new SourFldTpFileLoader();
                sourFldTpLoader.DataVessel = new SourFldTpContainer();
                sourFldTpLoader.TargetPath = UnionPay.Properties.Resources.SourFldTp;
                sourFldTpLoader.Load();
            }
            catch (System.Exception ex)
            {
                if (fmtLoader != null)
                {
                    fmtLoader.Close();
                }

                if (fldLoader != null)
                {
                    fldLoader.Close();
                }

                if (fldProccessLoader != null)
                {
                    fldProccessLoader.Close();
                }

                if (elemBussIdLoader != null)
                {
                    elemBussIdLoader.Close();
                }

                if (sourFldTpLoader != null)
                {
                    sourFldTpLoader.Close();
                }

                MessageBox.Show("File load error occures, please see log", "Error");
            }

        }
  
        static public DataContainer GetFmtData()
        {
            return fmtLoader.DataVessel;
        }

        static public DataContainer GetFldData()
        {
            return fldLoader.DataVessel;
        }

        static public DataContainer GetFldProcData()
        {
            return fldProccessLoader.DataVessel;
        }

        #region Fields
        //格式文件加载器
        static private FmtFileLoader fmtLoader;
        //字段文件加载器
        static private FldFileLoader fldLoader;
        //字段函数文件加载器
        static private FldProcessFileLoader fldProccessLoader;
        // 其他辅助文件加载器
        static private ElemBussIdFileLoader elemBussIdLoader;
        static private SourFldTpFileLoader sourFldTpLoader;
        #endregion
    }
}
